package com.cn.jdia.springmvc.handlers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.joda.time.DateTime;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;

@Controller
public class ProductController {

	@RequestMapping("/productdata")
	public String getJiaci(HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();

		@SuppressWarnings("resource")
		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("springmvc.xml");
		DataSource ds = (DataSource) w_context.getBean("dataSource2");

		DateTime datetime = new DateTime();
		String nowDate = datetime.toString("YYYY-MM-dd");

		String jiaci = null;
		String renci = null;
		String huoyou = null;
		String fangxing = null;
		String hangban=null;
		String con = "";

		try {
			Connection conn = ds.getConnection();
			Statement st = conn.createStatement();

			// StringBuffer sb = new StringBuffer(
			// "select FIELD0002,FIELD0003,FIELD0004 from FORMMAIN_6250 WHERE
			// to_char(START_DATE,'YYYY-MM-DD') like '%");
			StringBuffer sb = new StringBuffer(
					"select FIELD0002,FIELD0003,FIELD0004,FIELD0007,FIELD0006 from FORMMAIN_6250_AOC WHERE to_char(MODIFY_DATE,'YYYY-MM-DD') like '%");
			sb.append(nowDate).append("%'");

			ResultSet rs = st.executeQuery(sb.toString());
			while (rs.next()) {
				jiaci = rs.getString("FIELD0002");
				renci = rs.getString("FIELD0003");
				huoyou = rs.getString("FIELD0004");
				fangxing = rs.getString("FIELD0007");
				hangban = rs.getString("FIELD0006");
			}

			rs.close();

			String ysql = "select * from ATT_PECCANCY where CDATE='" + datetime.minusDays(1).toString("YYYY-MM-dd")
					+ "'";
			ResultSet yst = st.executeQuery(ysql);
			int count = 0;
			List<Map<String, String>> li = new ArrayList<Map<String, String>>();
			while (yst.next()) {
				String cdate = yst.getString("CDATE");
				String content = yst.getString("content");

				Map<String, String> map = new HashMap<String, String>();
				map.put("date", cdate);
				map.put("content", content);
				li.add(map);

				count++;
			}
			yst.close();

			System.out.println("count:" + count);
			if (count == 0) {

				String sql = "select * from ATT_PECCANCY where CDATE=(select CDATE from(select  * FROM ATT_PECCANCY ORDER BY CDATE DESC)WHERE ROWNUM <= 1)";
				// String sql = "select * from(select * FROM ATT_PECCANCY ORDER
				// BY CDATE DESC)WHERE ROWNUM <= 1";
				ResultSet rs2 = st.executeQuery(sql);
				// rs2.next();

				while (rs2.next()) {
					String cdate = rs2.getString("CDATE");
					String content = rs2.getString("content");

					Map<String, String> map = new HashMap<String, String>();
					map.put("date", cdate);
					map.put("content", content);
					li.add(map);
				}

				rs2.close();
			}

			con = gson.toJson(li);

			st.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, String> map = new HashMap<String, String>();
		map.put("jiaci", jiaci);
		map.put("renci", renci);
		map.put("huoyou", huoyou);
		map.put("fangxing", fangxing);
		map.put("hangban", hangban);
		map.put("con", con);

		String nowDate2 = datetime.minusDays(1).toString("YYYY年MM月dd日");
		map.put("date", nowDate2);

		String str = gson.toJson(map);
		out.write(str);
		out.flush();
		out.close();

		return null;
	}

	@RequestMapping("/chartdata")
	public String getJiaciChart(HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		DateTime datetime = new DateTime();
		DateTime oldDateTime = datetime.minusDays(4);
		String oldDate = oldDateTime.toString("YYYY-MM-dd");
		String nowDate = datetime.toString("YYYY-MM-dd");

		Map<String, List<String>> map = new HashMap<String, List<String>>();
		List<String> categories = new ArrayList<String>();
		List<String> data2 = new ArrayList<String>();
		List<String> data3 = new ArrayList<String>();
		List<String> data4 = new ArrayList<String>();
		List<String> data7 = new ArrayList<String>();

		@SuppressWarnings("resource")
		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("springmvc.xml");
		DataSource ds = (DataSource) w_context.getBean("dataSource2");
		try {
			Connection conn = ds.getConnection();
			Statement st = conn.createStatement();

			for (DateTime tmp = oldDateTime; tmp.isBefore(datetime.plus(1)); tmp = tmp.plusDays(1)) {
				System.out.println(tmp.toString("MM.dd"));
				categories.add(tmp.minusDays(1).toString("MM.dd"));

				// StringBuffer sql = new StringBuffer(
				// "select FIELD0002,FIELD0003,FIELD0004 from FORMMAIN_6250
				// WHERE to_char(START_DATE,'YYYY-MM-DD') like '%");
				StringBuffer sql = new StringBuffer(
						"select FIELD0002,FIELD0003,FIELD0004,FIELD0007 from FORMMAIN_6250_AOC WHERE to_char(MODIFY_DATE,'YYYY-MM-DD') like '%");
				sql.append(tmp.toString("YYYY-MM-dd")).append("%'");

				ResultSet rs = st.executeQuery(sql.toString());

				while (rs.next()) {
					String file2 = rs.getString("FIELD0002");
					String file3 = rs.getString("FIELD0003");
					String file4 = rs.getString("FIELD0004");
					String file7 = rs.getString("FIELD0007");
					data2.add(file2);
					data3.add(file3);
					data4.add(file4);
					data7.add(file7);
				}

				rs.close();
			}

			st.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		map.put("categories", categories);
		map.put("data2", data2);
		map.put("data3", data3);
		map.put("data4", data4);
		map.put("data7", data7);

		Gson gson = new Gson();
		String dataStr = gson.toJson(map);

		out.write(dataStr);
		out.flush();
		out.close();
		return null;
	}

}
